import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG, OPPO_AD_CONFIG } from '../../frame/constants';
import { UI_Listen, UI_Layer } from '../../config/ui_def'
cc.Class({
    extends: cc.Component,

    properties: {
        item_icon: cc.Sprite,
        item_title: cc.Label,
        item_detail: cc.Label,
    },
    initPage() {

    },

    updatePage() {
        let randomNumber = Math.floor(Math.random() * 12)//随机数
        if (randomNumber >= 1 && randomNumber <= 12) {
        }
        else {
            randomNumber = 1
        }
        let icon = (3000 + randomNumber)
        this.cardId = icon
        let id = icon * 10 + 1
        let item = he.db.getData("Item", id)
        let card = he.db.getData("Card", randomNumber * 5)
        this.item_title.string = `${item.name}`
        this.item_detail.string = `${card.description}`
        this.item_icon.spriteFrame = he.load.get('skill', cc.SpriteAtlas, 'plist').getSpriteFrame(`${icon}`)
        this.info = {
            type: 3,
            num: 1,
            name: item.name,
            description: card.description,
            icon: icon
        }
        this.adSwitch(true)
    },
    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    let guideList = he.local.getJson('guideList', 'user')
                    if (!guideList[6]) {
                        return
                    }
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'chayeAdFakeOPPO',
                        isShowAd: true,
                        isShowBanner: false,
                        adIndex: 11,
                        position: cc.Vec2(0, -570)
                    })
                    break
                case Sdk_Platform.UC:
                    he.ad.showBanner()
                    break
            }
        } else {
            //  关掉广告
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFakeOPPO')
                    break
                case Sdk_Platform.UC:
                    he.ad.closeBanner()
                    break
            }
        }
    },
    adSever() {
        switch (common.sdk_env) {
            case Sdk_Platform.OPPO:
                he.ad.showVideo(OPPO_AD_CONFIG.videoId[9], () => {
                    this.getCardServer()
                })
                break
            case Sdk_Platform.VIVO:
                he.ad.showVideo(() => {
                    this.getCardServer()
                })
                break
            case Sdk_Platform.UC:
                he.ad.showVideo(this.getCardServer.bind(this))
                break
            case Sdk_Platform.HUAWEI:
                he.ad.showVideo(this.getCardServer.bind(this))
                break
            default:
                this.getCardServer()
                break
        }
    },

    getCardServer() {
        let cardList = he.local.getJson('card', 'user')
        cardList.push({ id: this.cardId, lv: 1 })
        he.local.setJson('card', cardList, 'user')
        gg.eventManager.emit('card', { type: UI_Listen.SKILL_ADDCARD })
        this.uiBase.closePage()
        gg.uiManager.addPage(UI_Layer.Click, {
            name: 'getStoreItemPage',
            cover: true,
            info: this.info,
            double: true,
        })
    },

    closeUI() {
        this.adSwitch(false)
    },

    closeServer() {
        this.uiBase.closePage()
    },
});
